import {
  getMyDoctorList,
  getArticleList,
  getMyTeacherList
} from '../../api/index'
import {
  articleType,
  userType,
  findTypeByValue,
  findTypeByLabel
} from '../../utils/type';

const app = getApp()
Page({
  data: {
    active: 0,
    logined: false,
    baseCls: 'page_account',
    avatar_icon: '/assets/images/avatar.png',
    tab_active: 1,
    radio: '1',
    list: [],
    crossAxisCount: 2,
    crossAxisGap: 8,
    mainAxisGap: 8,
    key: '全部',
    isDoctor: false,
    MyAide: [],
    info: {
      doctor: {
        title: '我的医生',
        info: '医生'
      },
      teacher: {
        title: '我的患教',
        info: '患教'
      }
    },
    currInfo: null
  },
  onShow: function () {
    this.getTabBar().init();
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      let type = userInfo.type.label
      let isDoctor = type === '医生'
      this.setData({
        logined: true,
        show: false,
        userInfo: userInfo,
        isDoctor,
        currInfo: isDoctor ? this.data.info.teacher : this.data.info.doctor
      })
      this.ArticleList();
      this.getCode();
      this.getMyAide();
    }
  },
  //tab onch
  onChange(event) {

  },
  radio_onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  radio_onClick(event) {
    const {
      name
    } = event.currentTarget.dataset;
    this.setData({
      radio: name,
    });
  },

  // 滚动加载？
  bindscrolltolower() {
    
  },

  binderror(event) {
    this.setData({
      error: '错误' //event.detail.errMsg
    })
  },

  getMyAide(){
    let apiFunc = this.data.isDoctor ? getMyTeacherList : getMyDoctorList
    apiFunc().then(res => {
      this.setData({
        MyAide: res.rows
      })
    })
  },

  /**
   * 获取二维码
   * */
  getCode() {
    let that = this;
    let recordId = that.data.userInfo.id;
    wx.request({
      url: 'https://service.zn-care.com/api/getCodeQr',
      method: "POST",
      data: {
        scene: 'docker=' + recordId
      },
      success(res) {
        if (res.statusCode == 200) {
          that.setData({
            codeImg: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  // 保存图片
  saveImage(filePath) {
    var imgSrc = this.data.codeImg
    wx.downloadFile({
      url: imgSrc,
      success(res) {
        if (res.statusCode === 200) {
          // 保存图片到系统相册
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success(res) {
              wx.showToast({ // 添加成功提示框
                title: '保存图片成功',
                icon: 'success',
                duration: 2000
              });
            },
            fail(res) {
              wx.showToast({ // 添加失败提示框
                title: '保存图片失败',
                icon: 'none',
                duration: 2000
              });
            }
          });

        }
      }
    })

  },
  
  /**
   * 患教详情
   */
  patient_info(e) {
    var aideId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/aideInfo/index?aideId=' + aideId
    })
  },

  /* 资讯列表 */
  ArticleList(type) {
    let that = this;
    let recordId = this.data.userInfo.id;
    const params = {}
    if(type){
      params.type = type
    }
    
    getArticleList(params).then(res => {
      let allArticles = res.rows
      that.setData({
        ArticleList: allArticles,
        allArticle: allArticles
      })
      for (var i = 0; i < allArticles.length; i++) {
        var url = allArticles[i].urlList
        if (typeof (url) !== 'undefined') {
          var urlList = JSON.parse(url)
        } else {
          var urlList = ''
        }
        that.setData({
          ["ArticleList[" + i + "].imglist"]: urlList,
        })
      }
    })
  },
  /**
   * 资讯详情 
   */
  article_info(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/articleInfo/index?id=' + id
    })
  },
  /**
   * 资讯创建
   */
  articleAdd() {
    wx.navigateTo({
      url: '/pages/articleAdd/index'
    })
  },
  
  /**
   * 分配给我的任务key
   */
  articleTabChange(e) {
    if (typeof (e) !== 'undefined') {
      var key = e.currentTarget.dataset.key;
    } else {
      var key = this.data.key;
    }
    const type = findTypeByLabel(key, articleType)?.value
    this.ArticleList(type)
    this.setData({
      key
    })
  },
    /**
   * 跳转首页
   */
  onLogin() {
    if(!this.data.logined){
      wx.reLaunch({
        url: '/pages/index/index?login=true'
      })
    }
  },
})